package game.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ActionUtils {
	public static Map<String,String> getParamMap(String msg){
		String[] msgs = msg.split("&");
		Map data = new HashMap(msgs.length);
		for (int i = 0; i < msgs.length; i++) {
			String[] para = msgs[i].split("=");
			data.put(para[0], para.length>1?para[1]:null);
		}
		return data;
	}
	
	public static void setParamStr(StringBuffer json, Object obj){
		if(obj instanceof Map){
			Map map = (Map)obj;
			Iterator iter = map.keySet().iterator();
			while(iter.hasNext()){
				String key = iter.next().toString();
				json.append(key);json.append("=");
				setParamStr(json,map.get(key));
				if(iter.hasNext()){
					json.append("&");
				}
			}
		}else if(obj instanceof List){
			//json.append("[");
			List list = (List)obj;
			for (int i = 0; i < list.size(); i++) {
				setParamStr(json,list.get(i));
				if(i < list.size()-1){
					json.append("&");
				}
			}
			//json.append("]");
		}else if(obj instanceof java.util.Date || obj instanceof java.sql.Date || obj instanceof java.util.Calendar){
			json.append(DateUtils.toDatetime((Long)ToUtils.to(obj, Long.class)));
		}else{
			json.append(String.valueOf(obj));
		}
	}
}
